﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;

namespace NJL.WebUsers.AdminVisitor
{
    public partial class FarmerOrders :BasePage
    {
        BLL.NJL_BOOK_ORDER bll = new BLL.NJL_BOOK_ORDER();
        PagedDataSource pds;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["pager"] = "0";
                bind();
            }
        }
        void bind()
        {
            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
            pds = new PagedDataSource();
            DataSet ds = bll.GetList("CREATE_USER_ID='" + UserId + "' and NJL_BOOK_ID in (select ID from NJL_BOOK where TYPE_CODE='2')");
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 15;
            pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
            ViewState["endpage"] = pds.PageCount;
            lbtnFirst.Enabled = true;
            lbtnPrev.Enabled = true;
            lbtnNext.Enabled = true;
            lbtnEnd.Enabled = true;
            if (ViewState["pager"].ToString() == "0")
            {
                lbtnFirst.Enabled = false;
                lbtnPrev.Enabled = false;
            }
            if (Convert.ToInt32(ViewState["pager"]) == pds.PageCount - 1)
            {
                lbtnNext.Enabled = false;
                lbtnEnd.Enabled = false;
            }
            lit.Text = ds.Tables[0].Rows.Count.ToString();
            litAllPage.Text = pds.PageCount.ToString();
            litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            rptContent.DataSource = pds;
            rptContent.DataBind();
        }

        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }
        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();
        }
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();
        }
        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }
        NJL.BLL.NJL_BOOK bllBook = new BLL.NJL_BOOK();
        NJL.Model.NJL_BOOK modelBook;
        protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal litOrderState = (Literal)e.Item.FindControl("litOrderState");
                DataRowView drv = (DataRowView)e.Item.DataItem;
                HyperLink hyplink = (HyperLink)e.Item.FindControl("hplinkOrder");
                string bookId = drv["NJL_BOOK_ID"].ToString();
                string orderId = drv["ID"].ToString();
                int orderState = Convert.ToInt32(drv["ORDER_STATE"]);
                modelBook = bllBook.GetModel(bookId);
                if (modelBook.NJL_CS_BOOK_PAY_TYPE_CODE == "2")
                {
                    if (orderState == 1)
                    {
                        litOrderState.Text = "预订成功";
                    }
                }
                else if (modelBook.NJL_CS_BOOK_PAY_TYPE_CODE == "1")  //在线支付
                {
                    if (orderState == 2)
                    {
                        litOrderState.Text = "待支付状态";
                        hyplink.Visible = true;
                        hyplink.NavigateUrl = "/CompanyOrderPay.aspx?id=" + orderId;
                        hyplink.Target = "_blank";
                    }
                }
            }
        }
        protected void rptContent_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
    }
}